/*
 * 作者：姚俊
 * 功能：获奖报表
 * 日期：2015-05-24
 */
package leader.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Award;
import teacher.bean.Teacher;
import leader.service.DirectAwardService;
import tools.StringDate;

public class PullAwardToExcel {
	
	
	/**
	 * @category 生成获奖的报表
	 * @author yaojun
	 * @param flag int 值为0查询全部、1按照起止日期查询、2按照获奖名称模糊查询
	 * @param award1 Award 传入一个获奖的对象，当查询全部的时候该对象为空，当按照起止日期查询的时候，该对象必须有日期
	 * 当进行名称模糊查询的时候该对象中必须有名称
	 * @param award2  当是根据日期查询的时候，该对象传入一个截止日期
	 */
	public static void awardExcel(int flag,Award award1,Award award2,Teacher t)
	{
        //定义获奖列表
        List<Award> awards= null;
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"award.xls";
//            String fileName="D://awards.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0); 
               
               if(flag==0)
               {
            	   awards=new DirectAwardService().selectAllDirectAward(t);
               }
               else if(flag==1)
               {
            	   awards=new DirectAwardService().selectDirectAwardByTime(award1.getDate(), award2.getDate(), t);
               }
               else
               {
            	   awards=new DirectAwardService().selectAwardLikeName(award1.getName(), t);
               }

               //要插入到的Excel表格的行号，默认从0开始
               Label aid= new Label(0, 0, "编号");//表示第
               Label aname= new Label(1, 0, "获奖名称");//表示第
               Label aprojectName= new Label(2, 0, "获奖项目名称");
               Label alevel= new Label(3, 0, "获奖等级");   
               Label apeople= new Label(4, 0, "完成人");
               Label adate= new Label(5, 0, "获奖日期");
               
               ws.addCell(aid);
               ws.addCell(aname);
               ws.addCell(aprojectName);
               ws.addCell(alevel);
               ws.addCell(apeople);
               ws.addCell(adate);
               

               for (int i = 0; i < awards.size(); i++) {  
            	   String ee=null;
            	   Label aaid= new Label(0, i+1, i+1+"");
                   Label aaname= new Label(1, i+1, awards.get(i).getName()+"");
                   Label aaprojectName= new Label(2, i+1, awards.get(i).getProjectName());
                   if(awards.get(i).getLevel()==1){
                	   ee="一等奖";
                   }else if(awards.get(i).getLevel()==2){
                	   ee="二等奖";
                   }else if(awards.get(i).getLevel()==3){
                	   ee="三等奖";
                   }    
                   Label aalevel= new Label(3, i+1, ee+""); 
                   Label aapeople= new Label(4, i+1, awards.get(i).getPeople()+"");  
                   Label aadate= new Label(5, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   awards.get(i).getDate()))));                     
                   ws.addCell(aaid);
                   ws.addCell(aaname);
                   ws.addCell(aaprojectName);
                   ws.addCell(aalevel);          
                   ws.addCell(aapeople);
                   ws.addCell(aadate);
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }


}
